package com.zzty.txj1.controller;

import com.zzty.txj1.entity.User;
import com.zzty.txj1.util.GunZip;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TeamFileUploadController {
    @Value("${path.zip}")
    private String zipPath;
    @Value("${path.team}")
    private String teamPath;
    @RequestMapping(value = "/teamUpload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request)throws Exception {
        User user=(User)request.getSession().getAttribute("user");
        String message="";
        String userName=user.getName();
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss");
        String date=sdf.format(d);
        String zipName=userName+"-"+date+".zip";
        String unZipName=userName+"-"+date;
        if (!file.isEmpty()) {
            try {
                // 这里只是简单例子，文件直接输出到项目路径下。
                // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。
                // 还有关于文件格式限制、文件大小限制，详见：中配置。
                //File pathFile = new File("E://temp//zip//"+zipName);
                File pathFile = new File(zipPath+zipName);

                if(!pathFile.getParentFile().exists()){
                    pathFile.getParentFile().mkdirs();
                    if(pathFile.exists()){
                        pathFile.mkdir();
                    }
                }
                BufferedOutputStream out = new BufferedOutputStream(
                       // new FileOutputStream(new File("E://temp//zip//"+zipName)));
                new FileOutputStream(new File(zipPath+zipName)));

                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                message="上传失败," + e.getMessage();
                //return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
               // return "上传失败," + e.getMessage();
            }
            //GunZip.unZipFiles(new File("E://temp//zip//"+zipName),"E://temp//team//"+unZipName);
            GunZip.unZipFiles(new File(zipPath+zipName),teamPath+unZipName);
            //return "上传成功";
           // request.getSession().setAttribute("answerUrl","E://temp//team//"+unZipName);
            request.getSession().setAttribute("answerUrl",teamPath+unZipName);
            message="上传成功";
        } else {
            message="上传失败";
            //return "上传失败，因为文件是空的.";
        }
        return message;
    }

}
